package com.alibaba.csp.sentinel.cluster.client;

import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.spi.SpiLoader;

public final class TokenClientProvider {

    private static ClusterTokenClient client = null;

    static {
        // Not strictly thread-safe, but it's OK since it will be resolved only once.
        resolveTokenClientInstance();
    }

    public static ClusterTokenClient getClient() {
        return client;
    }

    private static void resolveTokenClientInstance() {
        ClusterTokenClient resolvedClient = SpiLoader.of(ClusterTokenClient.class).loadFirstInstance();
        if (resolvedClient == null) {
            RecordLog.info("[TokenClientProvider] No existing cluster token client, cluster client mode will not be activated");
        } else {
            client = resolvedClient;
            RecordLog.info("[TokenClientProvider] Cluster token client resolved: {}", client.getClass().getCanonicalName());
        }
    }

    public static boolean isClientSpiAvailable() {
        return getClient() != null;
    }

    private TokenClientProvider() {
    }
}
